package top.infopub.mgr.bpa.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.OtherCotAttDao;
import top.infopub.mgr.bpa.dao.OtherCotDao;
import top.infopub.mgr.bpa.dao.OtherCotDetailDao;
import top.infopub.mgr.bpa.dao.OtherCotListDao;
import top.infopub.mgr.bpa.dao.OtherCotNodeDao;
import top.infopub.mgr.bpa.dao.OtherCotSubDao;
import top.infopub.mgr.bpa.domain.OtherCotDomain;
import top.infopub.mgr.bpa.service.OtherCotService;
import top.infopub.mgr.bpa.vo.OtherCotVo;
import top.infopub.mgr.bpast.domain.BpastOtherDomain;
import top.infopub.mgr.flow.cnst.FlowTemplateEnum;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.bpa.OtherCot;
import top.infopub.model.bpa.OtherCotAtt;
import top.infopub.model.bpa.OtherCotDetail;
import top.infopub.model.bpa.OtherCotList;
import top.infopub.model.bpa.OtherCotNode;
import top.infopub.model.bpa.OtherCotSub;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


@Service
public class OtherCotServiceImpl extends GenericServiceImpl<OtherCot, String> implements OtherCotService {

    @Autowired
    private OtherCotDao otherCotDao;

    @Autowired
    private OtherCotSubDao otherCotSubDao;

    @Autowired
    private OtherCotDetailDao otherCotDetailDao;

    @Autowired
    private OtherCotNodeDao otherCotNodeDao;

    @Autowired
    private OtherCotAttDao otherCotAttDao;

    @Autowired
    private OtherCotListDao otherCotListDao;

    @Autowired
    private WsFlowProcessService wsFlowProcess;

    @Override
    public GenericDao<OtherCot, String> getDao() {
        return otherCotDao;
    }

    @Override
    public Page<OtherCotDomain> selectByDomainWhere(Page<OtherCotDomain> page,
                                                    OtherCotVo otherCotVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherCotDomain>)otherCotDao.selectByDomainWhere(otherCotVo);
    }

    @Override
    public Page<OtherCotDomain> selectMyCheckByDomainWhere(Page<OtherCotDomain> page,
                                                           OtherCotVo otherCotVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherCotDomain>)otherCotDao.selectMyCheckByDomainWhere(otherCotVo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOtherCot(OtherCot otherCot, List<OtherCotSub> otherCotSubs,
                             List<OtherCotList> otherCotLists,
                             List<OtherCotDetail> otherCotDetails,
                             List<OtherCotNode> otherCotNodes, List<OtherCotAtt> otherCotAtts) {
        //主表添加
        otherCotDao.insertById(otherCot);

        if (CollectionUtils.isNotEmpty(otherCotSubs)) {
            otherCotSubDao.insertByBatch(otherCotSubs);
        }

        if (CollectionUtils.isNotEmpty(otherCotDetails)) {
            otherCotDetailDao.insertByBatch(otherCotDetails);
        }

        if (CollectionUtils.isNotEmpty(otherCotNodes)) {
            otherCotNodeDao.insertByBatch(otherCotNodes);
        }

        if (CollectionUtils.isNotEmpty(otherCotLists)) {
            otherCotListDao.insertByBatch(otherCotLists);
        }

        if (CollectionUtils.isNotEmpty(otherCotAtts)) {
            otherCotAttDao.insertByBatch(otherCotAtts);
        }

        // 注意 这里根据不同的业务分支判断条件 获得不同enum
        FlowTemplateEnum templateEnum = FlowTemplateEnum.BPA_OTHER_COT;
        // 处理工作流数据
        wsFlowProcess.launch(templateEnum, otherCot.getId(), otherCot.getProcessId(), null);

    }

    @Override
    public List<OtherCot> isExistOtherCotName(String otherCotName,String proCode) {
        return otherCotDao.isExistOtherCotName(otherCotName,proCode);
    }

    @Override
    public List<OtherCot> isExistOtherCotSaName(String otherCotSaName,String proCode) {
        return otherCotDao.isExistOtherCotSaName(otherCotSaName,proCode);
    }

    @Override
    public OtherCotDomain selectNewsByProCode(String proCode) {
        return otherCotDao.selectNewsByProCode(proCode);
    }

    @Override
    public OtherCot selectaVersionByProCode(String proCode) {
        return otherCotDao.selectaVersionByProCode(proCode);
    }

    @Override
    public OtherCot selectByProCodes(String proCode) {
        return otherCotDao.selectByProCodes(proCode);
    }

    @Override
    public OtherCotVo queryById(String id) {
        return otherCotDao.queryById(id);
    }

    @Override
    public int countByName(String names) {
        return otherCotDao.countByName(names);
    }

    @Override
    public OtherCot selectAddTimeByProCode(String proCode) {
        return otherCotDao.selectAddTimeByProCode(proCode);
    }

    @Override
    public OtherCotDomain selectByIds(String id) {
        return otherCotDao.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sign(OtherCotVo otherCotVo, List<OtherCotAtt> otherCotAtts) {
        OtherCot otherCot = new OtherCot();
        otherCot.setId(otherCotVo.getId());
        otherCot.setSignDate(otherCotVo.getSignDate());
        otherCot.setUptBy(SessionUserUtils.getSessionUserId());
        otherCot.setUptTime(DateUtils.getCurrentDateTime());
        //更新记录
        otherCotDao.updateByIdSelective(otherCot);
        //补充附件
        if (CollectionUtils.isNotEmpty(otherCotAtts)) {
            otherCotAttDao.insertByBatch(otherCotAtts);
        }

    }

    @Override
    public List<OtherCot> selectByOtherCots() {
        return otherCotDao.selectByOtherCots(wsFlowProcess.proxyVisibleProCodes());
    }

    @Override
    public Page<BpastOtherDomain> selectBpastByDomainWhere(Page<BpastOtherDomain> page,
                                                                String type, String orgId,
                                                                String proName, String extType,
                                                                String contractName) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        Map<String, Object> where = new HashMap<String, Object>();
        where.put("proName", proName);
        where.put("contractName", contractName);
        where.put("extType", extType);
        if(type.equals("2")){//区域公司
            where.put("hrOrgAreaId", orgId);
        }else if(type.equals("3")){//项目公司
            where.put("hrOrgProId", orgId);
        }
        return (Page<BpastOtherDomain>)otherCotDao.selectBpastByDomainWhere(where);
    }
}